/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import controlador.exceptions.NonexistentEntityException;
import controlador.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import modelo.Laboratorio;
import modelo.Practica;

/**
 *
 * @author Fernando
 */
public class PracticaJpaController implements Serializable {

    public PracticaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Practica practica) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Laboratorio idLaboratorio = practica.getIdLaboratorio();
            if (idLaboratorio != null) {
                idLaboratorio = em.getReference(idLaboratorio.getClass(), idLaboratorio.getIdLaboratorio());
                practica.setIdLaboratorio(idLaboratorio);
            }
            em.persist(practica);
            if (idLaboratorio != null) {
                idLaboratorio.getPracticaCollection().add(practica);
                idLaboratorio = em.merge(idLaboratorio);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPractica(practica.getIdPractica()) != null) {
                throw new PreexistingEntityException("Practica " + practica + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Practica practica) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Practica persistentPractica = em.find(Practica.class, practica.getIdPractica());
            Laboratorio idLaboratorioOld = persistentPractica.getIdLaboratorio();
            Laboratorio idLaboratorioNew = practica.getIdLaboratorio();
            if (idLaboratorioNew != null) {
                idLaboratorioNew = em.getReference(idLaboratorioNew.getClass(), idLaboratorioNew.getIdLaboratorio());
                practica.setIdLaboratorio(idLaboratorioNew);
            }
            practica = em.merge(practica);
            if (idLaboratorioOld != null && !idLaboratorioOld.equals(idLaboratorioNew)) {
                idLaboratorioOld.getPracticaCollection().remove(practica);
                idLaboratorioOld = em.merge(idLaboratorioOld);
            }
            if (idLaboratorioNew != null && !idLaboratorioNew.equals(idLaboratorioOld)) {
                idLaboratorioNew.getPracticaCollection().add(practica);
                idLaboratorioNew = em.merge(idLaboratorioNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = practica.getIdPractica();
                if (findPractica(id) == null) {
                    throw new NonexistentEntityException("The practica with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Practica practica;
            try {
                practica = em.getReference(Practica.class, id);
                practica.getIdPractica();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The practica with id " + id + " no longer exists.", enfe);
            }
            Laboratorio idLaboratorio = practica.getIdLaboratorio();
            if (idLaboratorio != null) {
                idLaboratorio.getPracticaCollection().remove(practica);
                idLaboratorio = em.merge(idLaboratorio);
            }
            em.remove(practica);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Practica> findPracticaEntities() {
        return findPracticaEntities(true, -1, -1);
    }

    public List<Practica> findPracticaEntities(int maxResults, int firstResult) {
        return findPracticaEntities(false, maxResults, firstResult);
    }

    private List<Practica> findPracticaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Practica.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Practica findPractica(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Practica.class, id);
        } finally {
            em.close();
        }
    }

    public int getPracticaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Practica> rt = cq.from(Practica.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
